package writers_info;

import JdbcUtils.JDBCUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.jdbc.core.JdbcTemplate;

import java.net.URL;
import java.util.ArrayList;
import java.util.List;

public class Task {
    public static void main(String[] args) throws Exception {
        //获取template对象
        JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());
        //定义sql语句
        String sql = "insert into writers values (?,?,?,?)";
        //利用Jsoup获取每位诗人详情页url
        Document document = Jsoup.parse(new URL("https://so.gushiwen.cn/authors/"), 10000);
        Elements spans = document.select("#leftLuesuo > div > span");
        int count = 0;
        for (Element span : spans) {
            //得到每位诗人页面url
            String href = "https://so.gushiwen.cn" + span.select("a").attr("href");
            //得到诗人的名字
            String name = span.select("a").text();
            //输出提示符
            System.out.println(++count + "、正在爬取" + name + "...");
            //解析诗人详情页
            String details = null;
            Document doc = Jsoup.parse(new URL(href), 10000);
            details = doc.select("#sonsyuanwen > div.cont > p").text();
            details = details.split("►")[0];
            System.out.println(details);
            //创建writer对象
            Writer wr = new Writer();
            wr.setWriter(name);
            wr.setId(count);
            wr.setDetails(details);
            wr.setWurl(href);
            //创建writer_list集合
            List<Writer> list_wr = new ArrayList<Writer>();
            //如果writer不存在
            if(!list_wr.contains(wr)) {
                list_wr.add(wr);
                template.update(sql, wr.getId(), wr.getWriter(), wr.getDetails(), wr.getWurl());
                System.out.println(name + " 爬取完成！");
            } else {
                System.out.println(name + "异常！");
            }
        }
    }
}
